package demo.practice.leetcode_eveyday;

import java.util.Arrays;

public class P1046 {

    public static void main(String[] args) {
        P1046 p1046= new P1046();
        p1046.lastStoneWeight(new int[]{2,2});
    }
    public int lastStoneWeight(int[] stones) {

        if(stones.length==0)
            return 0;

        //2,7,4,1,8,1
        Arrays.sort(stones);
        //1,1,2,4,7,8

        for(int i=stones.length-1;i>=1;i--){
            int a1= stones[i];
            int a2=stones[i-1];
            if(a1!=a2){
                int insert= Math.abs(a1-a2);

                //此时棒当前的这个值插入到当前的数组中
                //1,1,2,4,_,_ 将1插入到这个有序队列
                stones[i-1]=insert;
                int j=i-2;
                for(;j>=0;j--){
                    if(stones[j]>insert ){
                        stones[j+1]=stones[j];
                        stones[j]=insert;
                    }
                }
            }else{
                stones[i-1]=0;
                i--;

            }
        }
        return stones[0];
    }
}
